<?php
/**
 * 赢销+投票消重
 * User: wangyongqiang
 * Date: 2017/7/3 15:12
 */
require_once './dbClass.php';
date_default_timezone_set('Asia/Shanghai');
class VoteDistinct{
    private $appid = 558583;
    private $_logpath = 'd:/votedistinct.log';
    private $_db;

    public function run(){
        if(file_exists($this->_logpath)){
            unlink($this->_logpath);
        }

        $this->_db = new dbClass('10.127.2.28', 'beeuser', 'U^M^uyyGHFpF4cWg', 'kingda_bee_7', '3306');

        //生成所有的区间日期
        $startTime = strtotime(date('Y-m-d 00:00:00', strtotime('2017/6/19')));
        $endTime = strtotime(date('Y-m-d 00:00:00', strtotime('2017/7/2')));
        $dayArr = $this->_getDateCha($startTime, $endTime);

        //传入日期，修改相应数据
        foreach ($dayArr as $day){
            $this->_changeByDay($day);
        }
    }

    private function _changeByDay($day){
        $dayTime = strtotime($day);
        $endDayTime = $dayTime + 86399;

        $where = " WHERE appid = {$this->appid} AND create_time > {$dayTime} AND create_time < {$endDayTime} ";
        $sql = "SELECT vote_id FROM `wp_sdk_voting_log` {$where} GROUP BY openid HAVING COUNT(vote_id) > 1;";
        $voteRes = $this->_db->query($sql);

        //遍历修改投票选手数据
        if($voteRes){
            $voteIds = array_unique(array_column($voteRes, 'vote_id'));
            foreach ($voteIds as $vote_id){
                $total = $this->_getRepeat($vote_id, $dayTime, $endDayTime);
                error_log($vote_id.'[total]'.$total."\r\n", 3, $this->_logpath);
            }
        }
    }

    private function _getRepeat($vote_id, $dayTime, $endDayTime){
        $where = " WHERE appid = {$this->appid} AND create_time > {$dayTime} AND create_time < {$endDayTime} ";
        //获取本天某选手重复的openid
        $where .= "  AND vote_id = {$vote_id} ";
        $sql2 = "SELECT * FROM `wp_sdk_voting_log` {$where} GROUP BY openid HAVING COUNT(openid) >1";
        error_log('[repeat openid sql]'.$sql2."\r\n", 3, $this->_logpath);

        $openidRes = $this->_db->query($sql2);

        $diffTotal = 0;
        $flag = false;
        if($openidRes){
            $openidArr = array_column($openidRes, 'openid');
            $openidUniArr = array_unique($openidArr);
            $openidStr = "'" . implode("','", $openidUniArr) . "'";

            //获取本天openid详情
            $where2 = " WHERE `status` = 1 AND appid = {$this->appid} AND create_time > {$dayTime} AND create_time < {$endDayTime}  AND vote_id={$vote_id} AND openid IN ($openidStr)";
            $sql3 = "SELECT * FROM `wp_sdk_voting_log` $where2";
            $res = $this->_db->query($sql3);

            $uniArr = $repeatArr = array();
            foreach ($res as $v){
                if(in_array($v['openid'], $uniArr)){
                    $repeatArr[] = $v['id'];
                }else{
                    $uniArr[] = $v['openid'];
                }
            }
            $diffTotal = count($repeatArr);
            if($diffTotal > 0){
                $idStr = implode(',', $repeatArr);
                error_log('[repeat openid openidStr]'.$openidStr."\r\n", 3, $this->_logpath);

                $flag = $this->_denyVoteLog($idStr);
            }
        }

        //修改选手数据
        if($diffTotal >0 && $flag !== false){
            $flag = $this->_updateVote($vote_id, $diffTotal);
        }

        return $flag;
    }

    /**
     * 修改投票
     * @param $vote_id
     * @param $diffTotal
     * @return bool
     */
    private function _updateVote($vote_id, $diffTotal){
        $sql1 = "UPDATE `wp_sdk_voting_data` set vote_count = vote_count -{$diffTotal} WHERE id={$vote_id};";
        error_log('[_updateVote]'.$sql1."\r\n", 3, $this->_logpath);

        $flag = $this->_db->execute($sql1);

        return $flag;
    }

    /**
     * 禁用状态
     * @param $logIdStr
     * @return mixed
     */
    private function _denyVoteLog($logIdStr){
        $sql2 = "UPDATE `wp_sdk_voting_log` set `status` = 0 WHERE id IN({$logIdStr});";
        error_log('[_denyVoteLog]'.$sql2."\r\n", 3, $this->_logpath);

        $flag = $this->_db->execute($sql2);

        return $flag;
    }

    /**
     * 获取所有日期数组
     * @param $start
     * @param $end
     * @return array
     */
    private function _getDateCha($start, $end){
        $cha = ceil(($end-$start)/86400);

        $data_arr = array(date('Y-m-d 00:00:00', $start));
        $beginDay = date('Y-m-d 00:00:00', $start);

        $tTime = strtotime($beginDay);
        $tDay = date('Y-m-d H:i:s', $tTime);

        for($i=1; $i < $cha; $i++){
            $data_arr[] = date('Y-m-d 00:00:00', strtotime("$beginDay +$i days"));
        }

        return $data_arr;
    }
}
$app = new VoteDistinct();
$app->run();